public class Main {
        // 第二十四天
        public int getMost(int[][] board) {
                int row = board.length;
                int col = board[0].length;
                for (int i = 1; i < col; i++) {
                        board[0][i] = board[0][i] + board[0][i - 1];
                }
                for (int i = 1; i < row; i++) {
                        board[i][0] = board[i][0] + board[i - 1][0];
                }

                for (int i = 1; i < row; i++) {
                        for (int j = 1; j < col; j++) {
                                int value = Math.max(board[i - 1][j], board[i][j - 1]);
                                board[i][j] = board[i][j] + value;
                        }
                }

                return board[row - 1][col - 1];
        }


        // 迷宫
        public static void main(String[] args) {
                Scanner scanner = new Scanner(System.in);
                int row = scanner.nextInt();
                int col = scanner.nextInt();

                int[][] array = new int[row][col];
                for (int i = 0; i < row; i++) {
                        for (int j = 0; j < col; j++) {
                                array[i][j] = scanner.nextInt();
                        }
                }
                List<List<Integer>> lists = new ArrayList<>();
                get(array, row, col, 0, 0, lists, 0);

                int size = array[row - 1][col - 1];
                for (int i = 0; i <= size; i++) {
                        System.out.println(lists.get(i));
                }
        }

        // DFS 一条道走到黑，一直向下走，走不通了；向右走，再向下走；都走不通了，回到上一步
        public static void get(int[][] array, int row, int col, int x, int y, List<List<Integer>> lists, int size) {
                if (x == row - 1 && y == col - 1) {
                        array[x][y] = size;
                }
                if (x < 0 || x >= row || y < 0 || y >= col) {
                        return;
                }

                if (array[x][y] == 1) {
                        return;
                }

                List<Integer> list = new ArrayList<>();
                list.add(x);
                list.add(y);
                lists.add(size, list);
                if (array[x][y] == 0) {
                        array[x][y] = 1;
                }
                size = size + 1;

                get(array, row, col, x + 1, y, lists, size);
                if (array[row - 1][col - 1] != 0) {
                        return;
                }
                get(array, row, col, x, y + 1, lists, size);
        }
}